package com.hdjk.view.activity.personalcenter;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdjk.view.R;
import com.hdjk.view.config.Constant;
import com.hdjk.view.util.QRCodeUtil;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.util.DensityUtil;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by sxy on 2017/11/15.
 */

public class ShareActivity extends BaseActivity implements View.OnClickListener {
    private String shareUrl = Constant.URL_PORT + "/User/Register?id=";
    private Bitmap bitmap = null;
    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private ImageView iv_share;
    private TextView tv_sure;
    // End Of Content View Elements

    private void bindViews() {

        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        iv_share = (ImageView) findViewById(R.id.iv_share);
        tv_sure = (TextView) findViewById(R.id.tv_sure);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_share);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_sure.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        bitmap = QRCodeUtil.createQRImageBitmap(shareUrl + UserTokenUtil.getUserName(this), DensityUtil.dip2px(this, 180), DensityUtil.dip2px(this, 180), null);
        if (bitmap != null)
            iv_share.setImageBitmap(bitmap);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_sure:
                try {
                    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                            Constant.YFHDCF_IMAGE_FILE_PATH);
                    if (!file.exists()) {
                        file.mkdirs();
                    }
                    File image = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +
                            Constant.YFHDCF_IMAGE_FILE_PATH, "myShareImage.jpg");
                    if (image.exists()) {
                        image.delete();
                    }
                    FileOutputStream out = new FileOutputStream(image);
                    Bitmap bm=getSaveBitmap();
                    bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    out.close();

                    MediaStore.Images.Media.insertImage(this.getContentResolver(), image.getAbsolutePath(), "myShareImage.jpg", null);
                    //保存图片后发送广播通知更新数据库
                    Uri uri = Uri.fromFile(image);
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
                    ToastUtils.showShortToast(this, "保存成功");
                } catch (Exception e) {
                    ToastUtils.showShortToast(this, "保存失败");
                    e.printStackTrace();
                }
                break;
        }
    }

    private Bitmap getSaveBitmap(){
        Bitmap bg= BitmapFactory.decodeResource(getResources(), R.drawable.share_save_bg);
        Bitmap saveBitMap=Bitmap.createBitmap(bg.getWidth(), bg.getHeight(),
                bg.getConfig());
        Canvas canvas = new Canvas(saveBitMap);
        canvas.drawBitmap(bg, new Matrix(), null);
        canvas.drawBitmap(bitmap, (bg.getWidth()-bitmap.getWidth())/2, 700, null);
        return saveBitMap;
    }
}
